package com.fun_ordering.controller;

import com.fun_ordering.components.ApiResponse;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.DishRatio;
import com.fun_ordering.service.DishRatioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * controller
 * @author XiaoPi
 *
 */
@RestController
@RequestMapping("/dishRatio")
public class DishRatioController {

	@Autowired
	private DishRatioService dishRatioService;

	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public ApiResponse<List<DishRatio>> findAll(){

		return ApiResponse.success(dishRatioService.findAll());
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){
		return dishRatioService.findPage(page, rows);
	}

	/**
	 * 增加
	 * @param dishRatio
	 * @return
	 */
	@RequestMapping("/add")
	public ApiResponse add(@RequestBody DishRatio dishRatio){
		try {
			dishRatioService.add(dishRatio);
		return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
		return ApiResponse.failed(null);
		}
	}

	/**
	 * 修改
	 * @param dishRatio
	 * @return
	 */
	@RequestMapping("/update")
	public ApiResponse update(@RequestBody DishRatio dishRatio){
		try {
			dishRatioService.update(dishRatio);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

	/**
	 * 获取实体
	 * @param ratioId
	 * @return
	 */
	@RequestMapping("/findOne")
	public DishRatio findOne(Integer ratioId){
		return dishRatioService.findOne(ratioId);
	}

	/**
	 * 批量删除
	 * @param ratioIds
	 * @return
	 */
	@RequestMapping("/delete")
	public ApiResponse delete(Integer [] ratioIds){
		try {
			dishRatioService.delete(ratioIds);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

		/**
	 * 查询+分页
	 * @param dishRatio
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody DishRatio dishRatio, int page, int rows  ){
		return dishRatioService.findPage(dishRatio, page, rows);
	}

}
